package com.xiao.sulinguser.controller;

import com.xiao.sulingpublic.utils.R;
import com.xiao.sulinguser.service.RegisterService;
import com.xiao.sulinguser.vo.UserSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author: 1322127758@qq.com
 * @date: 2022/3/13 16:51
 * @作用: 注册相关功能
 */
@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    /**
     * 用户提交注册请求
     * @Valid 数据效验注解
     * @param bindingResult 获取数据效验结果
     */
    @PostMapping("/submit")
    public R saveUser(@Valid UserSubmitVo userSubmitVo, BindingResult bindingResult){
        // 判断数据是否效验通过
        if(bindingResult.hasErrors()){
            // 数据效验不通过
            return R.error().put("error",bindingResult.getFieldError());
        }

        //注册
        String s = registerService.saveUser(userSubmitVo);

        return R.ok().put("data",s);
    }
}
